package com.study.cloudAlibaba.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.study.cloudAlibaba.dao.AccountDao;
import com.study.cloudAlibaba.domain.Account;
import com.study.cloudAlibaba.service.AccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author lzc
 * @date 2022/7/29 17:00
 */
@Service
public class AccountServiceImpl implements AccountService{
    private static final Logger LOGGER = LoggerFactory.getLogger(AccountServiceImpl.class);

    @Resource
    private AccountDao accountDao;

    @Override
    public void decrease(Long userId, BigDecimal money) {
        LOGGER.info("------->storage-service中扣减账户余额开始");
        LambdaQueryWrapper<Account> wrapper = new LambdaQueryWrapper<Account>().eq(Account::getUserId, userId);
        Account account = accountDao.selectOne(wrapper);
        account.setResidue(account.getResidue().subtract(money));
        account.setUsed(account.getUsed().add(money));
        accountDao.updateById(account);
        LOGGER.info("------->account-service中扣减账户余额结束");
    }

    @Override
    public List<Account> list() {
        return accountDao.selectList(null);
    }
}
